﻿#region  文件信息

// ---------------------------------------------------------------------------------------
// <copyright file="WxPackage.cs" company="">
// 作者：李帅伦
// </copyright>
// <summary>
// 类型简介：
// 类型详细：WxPackage.cs
// 创建时间：2014-12-25 12:58
// 修改时间：2014-12-23 15:47
// </summary>
// ---------------------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using AmazeUI.bll.Open.Weixin;
using AmazeUI.db;
using AmazeUI.Imodel;
using AmazeUI.Imodel.Res;
using Com;
using WebCommon;

namespace AmazeUI.bll.Open.WeixinPay
{
    #region



    #endregion

    /// <summary>
    /// The wx package.
    /// </summary>
    public class WxPackage : Wx
    {
        public static string Mchid = Config.GetString("wxmch_id");

        public static string Key = Config.GetString("wxkey");

        private string url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        #region Constructors and Destructors

        public IOrderToPay PayOrder;

        /// <summary>
        /// Initializes a new instance of the <see cref="WxPackage"/> class.
        /// </summary>
        /// <param name="payOrder">
        ///     The pay Service.
        /// </param>
        /// <param name="wxConfig">
        ///     The wx config.
        /// </param>
        /// <param name="openid">orderid</param>

        public WxPackage(IOrderToPay payOrder, string openid, string nourl)
        {
            this.d = new SortedDictionary<string, string>();
            this.PayOrder = payOrder;
             
            this.IsCredit = true;
            d.Add("appid", Wx.AppId);
            d.Add("mch_id", Mchid);
            d.Add("nonce_str", this.Noncestr);

            d.Add("body", PayOrder.Body);
            d.Add("out_trade_no", PayOrder.OrderNo);
            d.Add("total_fee", PayOrder.TotalFee.ToString("####"));
            d.Add("spbill_create_ip", payOrder.Ip);
            d.Add("notify_url", nourl);
            d.Add("trade_type", this.TradeType);
            d.Add("openid", openid);

        }

        #endregion
 

      

        /// <summary>
        /// 提交类型
        /// </summary>
        public string TradeType { get { return "JSAPI"; } }
        /// <summary>
        /// 是否能用信用卡
        /// </summary>
        public bool IsCredit { get; set; }

        /// <summary>
        /// 附加数据
        /// </summary>
        public string Attach { get; set; }
         

        /// <summary>
        /// 加入sign后， 获取提交url数据
        /// </summary>
        /// <returns></returns>
        public string Substring()
        {
            if (!IsCredit)
            {
                this.d.Add("limit_pay", "no_credit");
            }
            if (!string.IsNullOrWhiteSpace(Attach))
            {
                this.d.Add("attach", Attach);
            }

            var ob = WebCommon.StringCommand.Static.GetOrderBy(d);
            var sign = WxCommon.GetStringSignTemp(ob, Key);
            var dt = d.ToDictionary(s => s.Key, a => a.Value); 
            dt.Add("sign", sign);
            var data = (WebCommon.StringCommand.Static.ParseXml(dt)); 
            return data;
        }
       

        /// <summary>
        /// 提交订单信息集合
        /// </summary>
        private SortedDictionary<string, string> d;

        /// <summary>
        /// 写入其他订单集合
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public void SetSubOrder(string key, string val)
        {
            this.d.Add(key, val);
        }
        /// <summary>
        /// 获取提交订单信息集合
        /// </summary>
        public SortedDictionary<string, string> GetSubOrder
        {
            get { return this.d; }
        }

        /// <summary>
        /// 向服务器提交数据
        /// </summary>
        /// <returns></returns>
        public string PostUrl(string substring)
        {
            return HttpHelper.PostPage(this.url, substring);
        }

        public string GetPackageId(string retdata)
        {
            try
            {
                var sortedDictionary = WxCommon.FromXml(retdata);


                if (sortedDictionary["result_code"] == "SUCCESS" && sortedDictionary["return_code"] == "SUCCESS")
                {
                    return sortedDictionary["prepay_id"];
                }
                 
                return "";
            }
            catch (Exception)
            {

                return "";
            }

        }


    }
}